/*
 * Copyright (c) 2010-2020 Belledonne Communications SARL.
 *
 * This file is part of linphone-android
 * (see https://www.linphone.org).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.newlink.building.activities.main.settings.viewmodels

import androidx.lifecycle.MutableLiveData
import com.newlink.building.NL_App
import com.newlink.building.R
import com.newlink.building.activities.main.settings.Module_Phone_SettingListenerStub
import java.lang.NumberFormatException
import org.linphone.core.TransportType

class Module_Phone_NetworkSettingsViewModel : Module_Phone_GenericSettingsViewModel() {

    var originRportValue: Boolean? = false
    var originUdpKeepAlive: Boolean? = false

    val account = core.defaultAccount
    val wifiOnlyListener = object : Module_Phone_SettingListenerStub() {
        override fun onBoolValueChanged(newValue: Boolean) {
            core.isWifiOnlyEnabled = newValue
        }
    }
    val wifiOnly = MutableLiveData<Boolean>()

    val allowIpv6Listener = object : Module_Phone_SettingListenerStub() {
        override fun onBoolValueChanged(newValue: Boolean) {
            core.isIpv6Enabled = newValue
        }
    }
    val allowStrictProxyListener = object : Module_Phone_SettingListenerStub() {
        override fun onBoolValueChanged(newValue: Boolean) {
            core.setStrictProxyEnabled(newValue)
            NL_App.corePreferences.strictProxy = newValue
        }
    }

    val allowIpv6 = MutableLiveData<Boolean>()
    val allowStrictProxy = MutableLiveData<Boolean>()

    val randomPortsListener = object : Module_Phone_SettingListenerStub() {
        override fun onBoolValueChanged(newValue: Boolean) {
            val port = if (newValue) -1 else 5060
            setTransportPort(port)
            sipPort.value = port
            core.userportValue = newValue
            randomPorts.value = newValue
        }
    }
    val randomPorts = MutableLiveData<Boolean>()

    val sipPortListener = object : Module_Phone_SettingListenerStub() {
        override fun onTextValueChanged(newValue: String) {
            try {
                val port = newValue.toInt()
                setTransportPort(port)
            } catch (nfe: NumberFormatException) {
            }
        }
    }
    val sipPort = MutableLiveData<Int>()

    val udpKeepAlive = MutableLiveData<Boolean>()

    val udpKeepAliveListener = object : Module_Phone_SettingListenerStub() {
        override fun onBoolValueChanged(newValue: Boolean) {
            udpKeepAlive.value = newValue
//            try {
//                core.isKeepAliveEnabled = newValue
//            } catch (e: Exception) {
//                e.printStackTrace()
//            }
        }
    }

    // region Transport
    val proxy = MutableLiveData<String>()
    val transportListener = object : Module_Phone_SettingListenerStub() {
        override fun onListValueChanged(position: Int) {

            val params = account!!.params.clone()
            params.transport = TransportType.fromInt(position)
            account.params = params
            proxy.value = account.params.serverAddress?.asStringUriOnly()
        }
    }
    val transportIndex = MutableLiveData<Int>()
    val transportLabels = MutableLiveData<ArrayList<String>>()
    // Transport

    init {
        wifiOnly.value = core.isWifiOnlyEnabled
        allowIpv6.value = core.isIpv6Enabled
        udpKeepAlive.value = core.isKeepAliveEnabled
        allowStrictProxy.value = NL_App.corePreferences.strictProxy
        randomPorts.value = core.userportValue
        sipPort.value = getTransportPort()

        initOriginValue()
        initTransportList()
    }

    private fun initOriginValue() {
        originRportValue = randomPorts.value
        originUdpKeepAlive = udpKeepAlive.value
    }

    private fun setTransportPort(port: Int) {
        val transports = core.transports
        transports.udpPort = port
        transports.tcpPort = port
        transports.tlsPort = -1
        core.transports = transports
    }

    private fun getTransportPort(): Int {
        val transports = core.transports
        if (transports.udpPort > 0) return transports.udpPort
        return transports.tcpPort
    }

    fun savePreferenceChanged() {
        if (originRportValue != randomPorts.value) {
            val port = if (randomPorts.value!!) -1 else 5060
            setTransportPort(port)
            sipPort.value = port
            core.userportValue = randomPorts.value!!
            originRportValue = randomPorts.value
        }

        if (originUdpKeepAlive != udpKeepAlive.value) {
            try {
                core.isKeepAliveEnabled = udpKeepAlive.value!!
                originUdpKeepAlive = udpKeepAlive.value
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    fun checkPreferenceChanged(): Boolean {
        if (originRportValue != randomPorts.value) {
            return true
        } else if (originUdpKeepAlive != udpKeepAlive.value) {
            return true
        }
        return false
    }

    private fun initTransportList() {
        val labels = arrayListOf<String>()

        // Keep following in the same order as TransportType enum
        labels.add(prefs.getString(R.string.account_settings_transport_udp))
        labels.add(prefs.getString(R.string.account_settings_transport_tcp))
        labels.add(prefs.getString(R.string.account_settings_transport_tls))
        if (NL_App.corePreferences.allowDtlsTransport) {
            labels.add(prefs.getString(R.string.account_settings_transport_dtls))
        }

        transportLabels.value = labels
        transportIndex.value = account?.params?.transport?.toInt()
    }
}
